package bb.lanxing.activity.bluetooth;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import androidx.core.view.ViewCompat;
import bb.lanxing.R;
import bb.lanxing.activity.base.BaseActivity;
import bb.lanxing.manager.RemoteSharedPreference;
import bb.lanxing.manager.SPConstant;
import bb.lanxing.util.ui.InputHelper;
import java.util.ArrayList;
import java.util.List;
import kotlinx.coroutines.DebugKt;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class WheelLengthSetting extends BaseActivity {
    private WheelAdapter adapter;
    private EditText inputView;
    private ListView listView;
    private View mNextBtn;
    private TextView nextBtn;
    private LinearLayout searchView;
    private TextView titleView;
    private RelativeLayout topView;
    private float wheelDiameterLength;
    private final List<WheelLength> wheelLengths = new ArrayList<>();
    private final List<WheelLength> allWheels = new ArrayList<>();

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_wheel_length_setting);
        bindView(getWindow().getDecorView());
        this.titleView.setText(R.string.mine_device_setting_subtitle_wheel);
        this.nextBtn.setText(R.string.ok);
        if (getIntent().getBooleanExtra(DebugKt.DEBUG_PROPERTY_VALUE_AUTO, false)) {
            initWheelMap();
            this.searchView.setVisibility(View.GONE);
            return;
        }
        this.listView.setVisibility(View.GONE);
        this.baseHandler.postDelayed(() -> InputHelper.showSoftInput(inputView), 200L);
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }

    private void onNextClick() {
        String obj = this.inputView.getText().toString();
        InputHelper.hideSoftInput(this.inputView);
        if (!TextUtils.isEmpty(obj)) {
            try {
                float parseInt = Integer.parseInt(obj) / 1000.0f;
                this.wheelDiameterLength = parseInt;
                RemoteSharedPreference.getInstance().setValue(SPConstant.KEY_WHEEL_LENGTH, parseInt);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        Intent intent = new Intent();
        intent.putExtra("wheelDiameterLength", this.wheelDiameterLength);
        setResult(-1, intent);
        finish();
    }

    private void initWheelMap() {
        WheelAdapter wheelAdapter = new WheelAdapter(this.wheelLengths);
        this.adapter = wheelAdapter;
        this.listView.setAdapter((ListAdapter) wheelAdapter);
        try {
            for (Element element : new SAXReader().read(getAssets().open("tire.xml")).getRootElement().elements("dict")) {
                Element element2 = element.element("len");
                Element element3 = element.element("des");
                this.allWheels.add(new WheelLength(Integer.parseInt(element2.getText()), element3.getText()));
            }
            this.wheelLengths.addAll(this.allWheels);
            this.adapter.notifyDataSetChanged();
        } catch (Exception e) {
            e.printStackTrace();
        }
        int i = (int) (RemoteSharedPreference.getInstance().getFloat(SPConstant.KEY_WHEEL_LENGTH, 2.171f) * 1000.0f);
        for (int i2 = 0; i2 < this.wheelLengths.size(); i2++) {
            if (this.wheelLengths.get(i2).getLen() == i) {
                this.adapter.setSelected(i2);
            }
        }
    }

    private void bindView(View view) {
        nextBtn = (TextView) view.findViewById(R.id.nextBtn);
        titleView = (TextView) view.findViewById(R.id.titleView);
        topView = (RelativeLayout) view.findViewById(R.id.topView);
        inputView = (EditText) view.findViewById(R.id.inputView);
        searchView = (LinearLayout) view.findViewById(R.id.searchView);
        listView = (ListView) view.findViewById(R.id.listView);
        nextBtn.setOnClickListener(view2 -> onNextClick());
    }

    public class WheelAdapter extends BaseAdapter {
        private List<WheelLength> list;
        private int selected;

        @Override
        public long getItemId(int i) {
            return i;
        }

        WheelAdapter(List<WheelLength> list) {
            this.list = list;
        }

        @Override
        public int getCount() {
            return this.list.size();
        }

        @Override
        public Object getItem(int i) {
            return this.list.get(i);
        }

        @Override
        public View getView(final int i, View view, ViewGroup viewGroup) {
            ViewHolder viewHolder;
            if (view == null) {
                view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.wheel_len_item, viewGroup, false);
                viewHolder = new ViewHolder(view);
                view.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) view.getTag();
            }
            WheelLength wheelLength = this.list.get(i);
            TextView textView = viewHolder.lengthView;
            textView.setText(wheelLength.getLen() + " ");
            viewHolder.descriptionView.setText(wheelLength.getDes());
            if (i == this.selected) {
                viewHolder.containerView.setBackgroundResource(R.color.global_blue_color);
                viewHolder.descriptionView.setTextColor(-1);
                viewHolder.lengthView.setTextColor(-1);
            } else {
                viewHolder.containerView.setBackgroundResource(R.color.white);
                viewHolder.descriptionView.setTextColor(ViewCompat.MEASURED_STATE_MASK);
                viewHolder.lengthView.setTextColor(ViewCompat.MEASURED_STATE_MASK);
            }
            viewHolder.containerView.setOnClickListener(view2 -> WheelAdapter.this.setSelected(i));
            return view;
        }

        public void setSelected(int i) {
            this.selected = i;
            notifyDataSetChanged();
            WheelLength wheelLength = this.list.get(i);
            wheelDiameterLength = wheelLength.getLen() / 1000.0f;
            RemoteSharedPreference.getInstance().setValue(SPConstant.KEY_WHEEL_LENGTH, wheelLength.getLen() / 1000.0f);
        }
    }

    class ViewHolder {
        LinearLayout containerView;
        TextView descriptionView;
        TextView lengthView;
        public View view;

        public ViewHolder(View view) {
            this.view = view;
            this.lengthView = (TextView) view.findViewById(R.id.lengthView);
            this.descriptionView = (TextView) view.findViewById(R.id.descriptionView);
            this.containerView = (LinearLayout) view.findViewById(R.id.containerView);
        }
    }

    public static class WheelLength {
        private String des;
        private int len;

        WheelLength(int i, String str) {
            this.len = i;
            this.des = str;
        }

        public int getLen() {
            return this.len;
        }

        public String getDes() {
            return this.des;
        }

        public boolean equals(Object obj) {
            return (obj instanceof WheelLength) && ((WheelLength) obj).len == this.len;
        }
    }
}
